package GSEA::FDR::FdrCleaner ;


use strict ;
use warnings;

sub constrFdrCleaner($$$) #constructor
{
	my ($classe , $NormResultTable ,  $alpha ) = @_; 
	
	
	if( ! defined($NormResultTable 	)	){ die("argument 'NormResultTable' is not set , error");}	
	if( ! defined($alpha 	 		)	){ die("argument 'alpha' is not set , error");}

	
	my $this = {
					"NormResultTable"	=> $NormResultTable	 ,
					"alpha"				=> $alpha		 
				};
	
	bless ($this,$classe);  
		
	return $this; 
}

sub clean( $ )
{
	( my $this  ) = @_ ;
	
	my @NormResultTable = @{ $this->{"NormResultTable"} } ;
	
	@NormResultTable = sort { @{ $a  }[2] <=> @{ $b  }[2] }  @NormResultTable ;
	
	
	my $n 		= @NormResultTable ;
	my $alpha 	= $this->{"alpha"} ;
	
	my @return ;
	
	for( my $i = 0 ; $i < @NormResultTable ; $i++ )
	{
		my ( $name , $score , $NPvalue , $pt_courb , $pt_distrib  ) = @{ $NormResultTable[$i] } ;
		print("\n name=$name \t score=$score \t NPvalue=$NPvalue");
		
		my $limit = ( ($i+1) / ($n+1) )*$alpha  ;
		
		print("\t limit=$limit");
		
		if( $NPvalue < $limit )
		{
			my @info = ( $name , $score , $NPvalue * ( $n / ($i+1) ) , $pt_courb , $pt_distrib );
			push( @return , \@info );
		}
	}
	print("\n\n");
	return @return ;
}

1;